#include "Channel.h"
#include "Log.h"
#include <LoopThread.h>
#include <functional>
#include <memory>
#include <mutex>
#include <EventLoop.h>
// #include <pthread.h>
#include <pthread.h>
#include <thread>

namespace muduo{
    void LoopThread::Thread_Entry(){
        EventLoop loop;
        {
            // 
            std::unique_lock<std::mutex> lock(_mutex);
            _loop = &loop;
            _cond.notify_all();
        }
        loop.start();
    }
    LoopThread::LoopThread():_loop(nullptr),_thread(std::thread(&LoopThread::Thread_Entry,this)){}
    EventLoop* LoopThread::GetEventLoop(){
        // 想要获取loop，就要确保EventLoop已经实例化
        EventLoop* loop=nullptr;
        {
            std::unique_lock<std::mutex> lock(_mutex);
            // 如果不满足_loop!=nullptr，则释放锁，等待被通知之后获取锁
            _cond.wait(lock,[&](){return _loop!=nullptr;});
            loop=_loop;
        }
        return loop;
    }
}